<?php


namespace app\index\business;


use app\common\model\ConsignorItem;
use app\common\model\ConsignorItemAddMember;
use app\common\model\MemberInfo;

class sourceBus
{
    public $consignorItemModel = null;
    public $memberInfoMode = null;
    public $consignorItemAddMemberModel = null;
    public function __construct()
    {
        $this->consignorItemModel = new ConsignorItem();
        $this->memberInfoMode = new MemberInfo();
        $this->consignorItemAddMemberModel = new ConsignorItemAddMember();
    }
    public function getAllListPage($where,$paging,$whereTime,$title){
//        time();

        $res = $this->consignorItemModel->getListByPageWhere($where,$paging,$whereTime,$title);

        return $res;
    }
    public function getDetailBus($id){
        $sessionData = session(config('index.session_member'));
        if(!empty($sessionData) && $sessionData['member_grade']==2){
            $res = $this->consignorItemModel->getItemById($id);
            if(!empty($res)){
                $res->isShow = true;
            }
        }else{
           $res = $this->consignorItemModel->getItemFieldById($id);
            if(!empty($res)){
                $res->isShow = false;
            }
        }
        if(empty($res)){
            throw new \think\Exception('未发现该项目');
        }
        return $res;
    }
    public function applyConsignorItemBus($consignor_item_id,$member_id){
        $resultMemberInfo = $this->memberInfoMode->getMemberInfoById(['member_id'=>$member_id]);
        if(empty($resultMemberInfo)){
            throw new \think\Exception('该企业已注销或者正在审核状态，无法加入');
        }
        $repetitionWhere = [
            'consignor_item_id'=>$consignor_item_id,
            'member_id' => $member_id
        ];
        $result = $this->consignorItemAddMemberModel->getItem($repetitionWhere);
        if(!empty($result)){
            throw new \think\Exception('请勿重复申请');
        }
        $data = [
            'consignor_item_id'=>$consignor_item_id,
            'member_id' => $member_id,
            'member_info_name' => $resultMemberInfo['name']
        ];
        $res = $this->consignorItemAddMemberModel->add($data);
        return $res;
    }

    public function isApplyAddBus($consignor_item_id,$member_id){
        $repetitionWhere = [
            'consignor_item_id'=>$consignor_item_id,
            'member_id' => $member_id
        ];
        $result = $this->consignorItemAddMemberModel->getItem($repetitionWhere);

        return $result;
    }
}